﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QWeiBo.ViewModel;
using TencentWeiboSDK.Model;
using TencentWeiboSDK.Services;
using System.Threading;
using TencentWeiboSDK.Services.Util;

namespace QWeiBoN.ViewModule
{
    public class MainUserModel : BaseViewModel
    {

        /// <summary>
        /// 实例化帐户相关 API 服务.
        /// </summary>
        private UserService userService = new UserService();
        private User user;

        private string userHead;
        private string userNick;

        private string userId = string.Empty;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainUserModel()
        {
            user = new User();
            Console.WriteLine("");
        }

        /// <summary>
        /// 根据用户名得到用户资料
        /// </summary>
        /// <param name="name"></param>
        /// <param name="action"></param>
        public void getUserInfo(string name, Action<Callback<User>> action)
        {
            // 异步刷新，可以增加用户体验
            new Thread(() =>
            {
                if (string.IsNullOrWhiteSpace(name))
                {
                    // 获得自己的详细资料.
                    userService.UserInfo((userCallBack) =>
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            if (null != action)
                            {
                                action(userCallBack);
                            }
                        });
                    });
                }
                else
                {
                    // 获得别人的详细资料
                    userService.OtherInfo(new ServiceArgument() { Name = name }, (userCallBack) =>
                    {
                        // 切换并回调到UI线程.
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            if (null != action)
                            {
                                action(userCallBack);
                            }
                        });
                    });
                }
            });
        }

        /// <summary>
        /// 刷新时间线需要的数据
        /// </summary>
        /// <param name="action"></param>
        public void RefreshUser(Action action)
        {
            // 异步刷新可以让用户有更好的操作体验
            new Thread(() =>
            {
                if (userId == string.Empty)
                {
                    // 获得自己的详细资料.
                    userService.UserInfo((userCallBack) =>
                    {
                        // Callback 结果是当前 AccessToken 的用户数据
                        // 可以参考 UserInfo 的定义: UserInfo(Action<Callback<User>> callback)
                        this.User = userCallBack.Data;

                        // 切换并回调到UI线程.
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            if (null != action)
                            {
                                action();
                            }
                        });
                    });
                }
                else
                {
                    // 获得别人的详细资料
                    userService.OtherInfo(new ServiceArgument() { Name = userId }, (userCallBack) =>
                    {
                        this.User = userCallBack.Data;
                        // 切换并回调到UI线程.
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            if (null != action)
                            {
                                action();
                            }
                        });
                    });
                }
            }).Start();
        }

        public User User
        {
            get { return user; }
            set
            {
                if (user != value)
                {
                    user = value;
                    if (null == user.Head || user.Head.Equals(""))
                    {
                        UserHead = "Resources/head_100.jpg";
                        user.Head = "Resources/head_100.jpg";
                    }
                    else
                    {
                        UserHead = user.Head;
                    }
                    UserNick = user.Nick;
                    NotifyPropertyChanged("User");
                }
            }
        }

        public string UserHead
        {
            get { return userHead; }
            set
            {
                userHead = value;
            }
        }

        public string UserNick
        {
            get { return userNick; }
            set
            {
                userNick = value;
                NotifyPropertyChanged("UserNick");
            }
        }

        public string UserId
        {
            get { return userId; }
            set { userId = value; }
        }
    }
}
